package  com.sat.common.widget.commom

import android.util.SparseArray
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView

/**
 * ViewHolder工具类
 *
 * @author DavenXie
 * Created by Daven on 2018/7/5.
 */
class ViewHolderHelper private constructor(val convertView: View) {
    private val views: SparseArray<View?> = SparseArray()
    fun <T : View?> getView(viewId: Int): T? {
        var view = views[viewId]
        if (view == null) {
            view = convertView.findViewById(viewId)
            views.put(viewId, view)
        }
        return view as T?
    }

    fun setText(viewId: Int, text: String?): ViewHolderHelper {
        val textView = getView<TextView>(viewId)!!
        textView.text = text
        return this
    }

    fun getText(viewId: Int): String {
        val textView = getView<TextView>(viewId)!!
        return textView.text.toString()
    }

    fun setText(viewId: Int, textId: Int): ViewHolderHelper {
        val textView = getView<TextView>(viewId)!!
        textView.setText(textId)
        return this
    }

    fun setTextColor(viewId: Int, colorId: Int): ViewHolderHelper {
        val textView = getView<TextView>(viewId)!!
        textView.setTextColor(colorId)
        return this
    }

    fun setTextGravity(viewId: Int, gravity: Int): ViewHolderHelper {
        val textView = getView<TextView>(viewId)!!
        textView.gravity = gravity
        return this
    }

    fun setButtonText(viewId: Int, textId: String?): ViewHolderHelper {
        val btn = getView<Button>(viewId)!!
        btn.text = textId
        return this
    }

    fun getButtonText(viewId: Int): String {
        val button = getView<Button>(viewId)!!
        return button.text.toString()
    }

    fun setButtonTextColor(viewId: Int, colorId: Int): ViewHolderHelper {
        val btn = getView<Button>(viewId)!!
        btn.setTextColor(colorId)
        return this
    }

    fun setOnClickListener(
        viewId: Int,
        clickListener: (View)->Unit
    ): ViewHolderHelper {
        val view = getView<View>(viewId)!!
        view.setOnClickListener(clickListener)
        return this
    }

    fun setBackgroundResource(viewId: Int, resId: Int): ViewHolderHelper {
        val view = getView<View>(viewId)!!
        view.setBackgroundResource(resId)
        return this
    }

    fun setBackgroundColor(viewId: Int, colorId: Int): ViewHolderHelper {
        val view = getView<View>(viewId)!!
        view.setBackgroundColor(colorId)
        return this
    }

    /**
     * 为对应ImageView控件设置Resource
     * @param viewId 控件id
     * @param resId 资源id
     * @return ViewHolderHelper
     */
    fun setImageResource(viewId: Int, resId: Int): ViewHolderHelper {
        val view = getView<ImageView>(viewId)!!
        view.setImageResource(resId)
        return this
    }

    /**
     * 为对应控件设置是否显示
     * @param viewId 控件id
     * @return ViewHolderHelper
     */
    fun setVisibility(viewId: Int, visibility: Int): ViewHolderHelper {
        val view = getView<View>(viewId)!!
        view.visibility = visibility
        return this
    }

    companion object {
        fun create(view: View): ViewHolderHelper {
            return ViewHolderHelper(view)
        }
    }

}